package com.cgw.web.base.action;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.cgw.common.base.BaseAction;
import com.cgw.common.util.help.CookieUtils;
import com.cgw.dmplt.address.domain.Helper;
import com.cgw.dmplt.base.dao.BaseRentpriceDao;
import com.cgw.dmplt.base.dao.BaseRoomtypeDao;
import com.cgw.dmplt.base.domain.BaseRentprice;
import com.cgw.dmplt.base.domain.BaseRoomtype;

public class BaseDataAction extends BaseAction {

	private BaseRentprice rentPrice;
	private BaseRoomtype roomType;
	private String basePrice;
	HttpServletResponse response = ServletActionContext.getResponse();
	HttpServletRequest request = ServletActionContext.getRequest();
	
	public String doList(Helper m){
		BaseRentpriceDao BaseRentpriceDao=(BaseRentpriceDao)getBean("baseRentPriceDao");
		BaseRoomtypeDao BaseRoomtypeDao=(BaseRoomtypeDao)getBean("baseRoomTypeDao");
		StringBuilder sb = new StringBuilder();
		try{
			if(rentPrice==null){
				rentPrice=new BaseRentprice();
			}
			if(m==null){
				m=new Helper();
			}
			List<BaseRentprice> BaseRentpriceList=BaseRentpriceDao.listBaseRentprice(rentPrice);
			if(roomType==null){
				roomType=new BaseRoomtype();
			}
			sb.append("<div class=\"filter_container\">");
			// 价格
			String strPriceID=m.getRentPriceID();
//			HttpSession session = ServletActionContext.getRequest().getSession(); 
//			if(m.getRentPriceID()!=null){
//				if(session.getAttribute(m.getRentPriceID())!=null){				
//	//				strPriceID=CookieUtils.getCookieByName(ServletActionContext.getRequest(), m.getRentPriceID());
//					strPriceID=session.getAttribute(m.getRentPriceID()).toString();
//					System.out.println("-----if----session_-------"+strPriceID);
//				}else{
//					strPriceID=session.getAttribute("sRentPriceID").toString();
//				}
//			}
			if (strPriceID== null || strPriceID.equals("")) 
				sb.append("<div class=\"SearchBar\"><ul><li>售价：</li><li class=\"SearchNoRestrict\">不限</li>");
			else
				sb.append("<div class=\"SearchBar\"><ul><li>售价：</li><li><a class=\"SearchStrStyle\" onclick=\"fixPrice('','','','');\" href=\"javascript:void(0)\">不限</a></li>");   
			
			for (BaseRentprice var : BaseRentpriceList) {
				if (strPriceID!= null && strPriceID.equals(var.getRentid()))
				{
					sb.append(String.format("<li><a class=\"SearchNoRestrict\" onclick=\"fixPrice('%1$s','%2$s','%3$s');\" href=\"javascript:void(0)\">%4$s</a></li>",  var.getRentfrom(), var.getRentend(), var.getRentid(), var.getRenttitle()));
				}
				else
				{
					sb.append(String.format("<li><a class=\"SearchStrStyle\" onclick=\"fixPrice('%1$s','%2$s','%3$s');\" href=\"javascript:void(0)\">%4$s</a></li>", var.getRentfrom(), var.getRentend(), var.getRentid(), var.getRenttitle()));
				}
			}
			sb.append("</ul></div>");
			
			//户型
			List<BaseRoomtype> BaseRoomtypeList=BaseRoomtypeDao.listBaseRoomtype(roomType);
//			String strRoomID = session.getAttribute(m.getRoomTypeID()).toString();
			String strRoomID = m.getRoomTypeID();
			if (strRoomID==null || strRoomID.equals(""))
			{
				sb.append("<div class=\"SearchBar\"><ul><li>房型：</li><li class=\"SearchNoRestrict\">不限</li>");
			}
			else
			{
				sb.append("<div class=\"SearchBar\"><ul><li>房型：</li><li><a class=\"SearchStrStyle\" onclick=\"fixRomm('','');\" href=\"javascript:void(0)\">不限</a></li>");
			}

			for (BaseRoomtype var : BaseRoomtypeList) {
				if (strRoomID!= null && strRoomID.equals(var.getTypeid()))
				{
					sb.append(String.format("<li><a class=\"SearchNoRestrict\" onclick=\"fixRomm('%1$s','%2$s');\" href=\"javascript:void(0)\">%3$s</a></li>", var.getTypeid(), var.getRoomnum(), var.getRoomtitle()));
				}
				else
				{
					sb.append(String.format("<li><a class=\"SearchStrStyle\" onclick=\"fixRomm('%1$s','%2$s');\" href=\"javascript:void(0)\">%3$s</a></li>", var.getTypeid(), var.getRoomnum(), var.getRoomtitle()));
				}
			}
			sb.append("</ul></div>");

			sb.append("</div>");

			setBasePrice(sb.toString());
		}catch(Exception e){
			e.printStackTrace();
			log.error("列表查询出错!", e);
		}
//		return SUCCESS;
		return sb.toString();
	}

	public BaseRentprice getRentPrice() {
		return rentPrice;
	}

	public void setRentPrice(BaseRentprice rentPrice) {
		this.rentPrice = rentPrice;
	}

	public BaseRoomtype getRoomType() {
		return roomType;
	}

	public void setRoomType(BaseRoomtype roomType) {
		this.roomType = roomType;
	}
	
	public String getBasePrice() {
		return basePrice;
	}

	public void setBasePrice(String basePrice) {
		this.basePrice = basePrice;
	}
}
